package com.webware.one.client.ui;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.layout.client.Layout.AnimationCallback;
import com.google.gwt.layout.client.Layout.Layer;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.webware.one.client.Site;

public class Dialog {
  public enum EDialogButton {
    OK, CANCEL, YES, NO
  };

  public interface ICloseDialogHandler {
    public void onClick(DialogBox sender, EDialogButton button);
  }

  private static int hintCount = 0;

  public static void confirm(String confirmHtml, final ICloseDialogHandler handler) {
    final DialogBox dialog = new DialogBox();
    HTMLPanel panel = new HTMLPanel(
      "<table id=dlgTable><tr style=\"height: 70px;\"><td id=dlgText class=dlgText></td></tr></table><div id=dlgButtons class=dlgButtons></div>");
    dialog.add(panel);
    dialog.setAnimationEnabled(true);
    dialog.setStyleName("dialog");
    dialog.setGlassEnabled(true);
    dialog.setModal(true);

    dialog.setText(Site.messages.msgAlert());
    panel.getElementById("dlgTable").setClassName("dlgConfirm");
    panel.getElementById("dlgText").setInnerHTML(confirmHtml);

    Button button = new Button(Site.messages.msgOk(), new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        dialog.setLayoutData(EDialogButton.OK);
        dialog.hide();
      }
    });
    button.setStyleName("dlgButton");
    panel.add(button, panel.getElementById("dlgButtons"));

    button = new Button(Site.messages.msgCancel(), new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        dialog.setLayoutData(EDialogButton.CANCEL);
        dialog.hide();
      }
    });
    button.setStyleName("dlgButton cancel");
    panel.add(button, panel.getElementById("dlgButtons"));

    panel.setWidth("300px");
    dialog.center();
    dialog.addCloseHandler(new CloseHandler<PopupPanel>() {
      @Override
      public void onClose(CloseEvent<PopupPanel> event) {
        handler
          .onClick((DialogBox) event.getSource(), (EDialogButton) ((PopupPanel) event.getSource()).getLayoutData());
      }
    });
  }

  private final static int HINT_WIDTH = 250;
  private final static int HINT_HEIGHT = 80;

  public static void hint(String text) {
    final int y = hintCount * (HINT_HEIGHT + 10);
    hintCount++;

    final HTML panel = new HTML("<div style='overflow: hidden; margin: 10px; height: " + (HINT_HEIGHT - 20) + "px;'>"
        + text + "</div>");
    panel.setStyleName("dialog");

    RootLayoutPanel.get().add(panel);
    RootLayoutPanel.get().setWidgetRightWidth(panel, 30 + HINT_WIDTH / 2, Unit.PX, 0, Unit.PX);
    RootLayoutPanel.get().setWidgetBottomHeight(panel, 30 + y + HINT_HEIGHT / 2, Unit.PX, 0, Unit.PX);
    RootLayoutPanel.get().forceLayout();

    RootLayoutPanel.get().setWidgetRightWidth(panel, 30, Unit.PX, HINT_WIDTH, Unit.PX);
    RootLayoutPanel.get().setWidgetBottomHeight(panel, 30 + y, Unit.PX, HINT_HEIGHT, Unit.PX);
    RootLayoutPanel.get().animate(300);

    Timer tm = new Timer() {
      @Override
      public void run() {
        RootLayoutPanel.get().forceLayout();
        RootLayoutPanel.get().setWidgetRightWidth(panel, 30 + HINT_WIDTH / 2, Unit.PX, 0, Unit.PX);
        RootLayoutPanel.get().setWidgetBottomHeight(panel, 30 + y + HINT_HEIGHT / 2, Unit.PX, 0, Unit.PX);
        RootLayoutPanel.get().animate(300, new AnimationCallback() {
          @Override
          public void onLayout(Layer layer, double progress) {}

          @Override
          public void onAnimationComplete() {
            if (--hintCount < 0) hintCount = 0;
            panel.removeFromParent();
          }
        });
      }
    };
    tm.schedule(2000);
  }

  public static void alertHint(String text) {
    final int y = hintCount * (HINT_HEIGHT + 10);
    hintCount++;

    final HTML panel = new HTML("<div style='overflow: hidden; margin: 10px; height: " + (HINT_HEIGHT - 20) + ";'>"
        + text + "</div>");
    panel.setStyleName("dialog red");

    RootLayoutPanel.get().add(panel);
    RootLayoutPanel.get().setWidgetRightWidth(panel, 30 + HINT_WIDTH / 2, Unit.PX, 0, Unit.PX);
    RootLayoutPanel.get().setWidgetBottomHeight(panel, 30 + y + HINT_HEIGHT / 2, Unit.PX, 0, Unit.PX);
    RootLayoutPanel.get().forceLayout();

    RootLayoutPanel.get().setWidgetRightWidth(panel, 30, Unit.PX, HINT_WIDTH, Unit.PX);
    RootLayoutPanel.get().setWidgetBottomHeight(panel, 30 + y, Unit.PX, HINT_HEIGHT, Unit.PX);
    RootLayoutPanel.get().animate(300);

    Timer tm = new Timer() {
      @Override
      public void run() {
        RootLayoutPanel.get().forceLayout();
        RootLayoutPanel.get().setWidgetRightWidth(panel, 30 + HINT_WIDTH / 2, Unit.PX, 0, Unit.PX);
        RootLayoutPanel.get().setWidgetBottomHeight(panel, 30 + y + HINT_HEIGHT / 2, Unit.PX, 0, Unit.PX);
        RootLayoutPanel.get().animate(300, new AnimationCallback() {
          @Override
          public void onLayout(Layer layer, double progress) {}

          @Override
          public void onAnimationComplete() {
            if (--hintCount < 0) hintCount = 0;
            panel.removeFromParent();
          }
        });
      }
    };
    tm.schedule(5000);
  }

  private final static HTML glass = new HTML();

  public static Widget waiting() {
    if (glass.isAttached()) return glass;

    glass.setStyleName("dlgWaiting");
    RootLayoutPanel.get().add(glass);
    RootLayoutPanel.get().setWidgetLeftRight(glass, 0, Unit.PX, 0, Unit.PX);
    RootLayoutPanel.get().setWidgetTopBottom(glass, 0, Unit.PX, 0, Unit.PX);

    return glass;
  }
}
